package org.callisto.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;

import org.callisto.EventType;
import org.callisto.StatisticType;
import org.callisto.model.Event;
import org.callisto.model.Keyboard;

public class PlotterCanvas extends Widget {
	
	private static final long serialVersionUID = -5233551458868531650L;
	EventType eventType;
	int windowPixWitdh;
	int windowPixHeight;
	
	public PlotterCanvas(Keyboard keyboard, EventType eventType, int width, int height) {
		super(keyboard, eventType, width, height);
		setLayout(new FlowLayout());
		setPreferredSize(new Dimension(400,200));
		setBackground(new Color(0x40,0x40,0x40));
	}
	
	private void drawStatLine(Graphics2D g2, float measure, float maxMeasure, Color color) {
		int lineheight = 0;
		lineheight = (int) (windowPixHeight * measure / maxMeasure);
		int summaryPixY = windowPixHeight - lineheight;
		g2.setColor(color);
		g2.drawLine(0,summaryPixY,windowPixWitdh,summaryPixY);
		//Globals.logln(String.format("(measure,max,angle,tipX,tipY)=(%.2f,%.2f,%.2f,%.2f,%.2f)\n",measure,maxMeasure,angle,tipX,tipY));
}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		if (stroke_index>0) {
			// get pixel dimensions for drawing area
			windowPixWitdh = getWidth();
			windowPixHeight = getHeight();
			g.setColor(Color.BLACK);
			g.drawRect(0, 0, windowPixWitdh, windowPixHeight);
			final int eventPixWidth = 10;
			int windowEventWidth = windowPixWitdh / eventPixWidth;
			// get index boundaries for displayed events
			int eventCount = keyboard.getEventCount(eventType);
			int windowEventMin = (eventCount < windowEventWidth) ? 0 : eventCount - windowEventWidth;
			int windowEventMax = eventCount;
			float maxMeasure = keyboard.getMeasureStatistic(eventType, StatisticType.MAX);
			for (int i=windowEventMin; i<windowEventMax; i++) {
				Event event = keyboard.getEvent(eventType,i);
				int renderEventIndex = i - windowEventMin;
				int currentMeasure=event.getMeasure();
				maxMeasure = (currentMeasure > maxMeasure) ? currentMeasure : maxMeasure;
				((Plotter) getParent()).setHi((float) maxMeasure);
				((Plotter) getParent()).setMid(maxMeasure/2f);
				((Plotter) getParent()).setLo(0f);
				int eventPixX = renderEventIndex * eventPixWidth;
				int barheight=0;
				barheight=(int) (windowPixHeight  * currentMeasure / maxMeasure);
				final int MAX_NOTE=108;
				final int MIN_NOTE=21;
				g2.setColor(new Color(0xaaaaff));
				g2.fillRect(eventPixX,windowPixHeight - barheight,eventPixWidth,barheight);
				g2.setColor(new Color(0x00c000));
				int notePixY= (int)((float)(event.getNote() - MIN_NOTE) / (MAX_NOTE - MIN_NOTE) * windowPixHeight );
				g2.fillOval(
						eventPixX + eventPixWidth / 2, 
						windowPixHeight - notePixY, 
						8, 4);
	//					Globals.logln(
	//								"note=" + event.getNote() + 
	//								",type=" + event.getStatus() + ", i=" + i + ", renderIndex=" + renderEventIndex +
	//								", currentMeasure=" + currentMeasure + ", maxMeasure=" + maxMeasure +
	//								", leftx=" + eventPixX + ", eventPixWidth=" + eventPixWidth + ", barheight=" + barheight +
	//								", pheight=" + notePixY
	//								);
			}
			// *** Draw summary line
			
			Event lastEvent = keyboard.getLastEvent(eventType);
			if (lastEvent != null) {
				float currentMeasure = lastEvent.getMeasure();
				float minMeasure = keyboard.getMeasureStatistic(eventType, StatisticType.MIN);
				float meanMeasure = keyboard.getMeasureStatistic(eventType, StatisticType.MEAN);
				float medianMeasure = keyboard.getMeasureStatistic(eventType, StatisticType.MEDIAN);
				drawStatLine(g2, currentMeasure, maxMeasure, Color.WHITE);
				drawStatLine(g2, minMeasure, maxMeasure, Color.RED);
				drawStatLine(g2, maxMeasure, maxMeasure, Color.RED);
				drawStatLine(g2, medianMeasure, maxMeasure, Color.YELLOW);
				drawStatLine(g2, meanMeasure, maxMeasure, Color.ORANGE);
			}
		}
	}
}
